package org.lemon.transport.context.service;

import lombok.extern.slf4j.Slf4j;
import org.lemon.transport.context.FSTUtils;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Service
public class ProtoWithFSTService implements DataDecodingEncodingService {

    @Override
    public <T> Optional<T> decode(byte[] byteArray) {
        try {
            return Optional.ofNullable(FSTUtils.decode(byteArray));
        } catch (IllegalArgumentException e) {
            log.error("Error during deserialization message, [{}]", e.getMessage());
            return Optional.empty();
        }
    }

    @Override
    public <T> byte[] encode(T msq) {
        return FSTUtils.encode(msq);
    }
}
